package com.ywps.vaas.framework.util
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import org.slf4j.{Logger, LoggerFactory}

import java.net.URLEncoder
import scala.collection.mutable
import scala.io.Source

object WrapperHttpUtils {
  val logger: Logger = LoggerFactory.getLogger(this.getClass)
  var httpsRequestTimes =0


  def sendGetRequest(url: String, paramMap: mutable.Map[String, String]): String = {
    var paramStr = ""
    var httpUrl: String = url;
    val keySet: collection.Set[String] = paramMap.keySet
    try {
      if (keySet.nonEmpty) {
        keySet.foreach(key => paramStr = paramStr.concat("&" + key + "=" + URLEncoder.encode(paramMap.get(key).get, "UTF-8")))
        val params: String = paramStr.substring(1, paramStr.length)
        httpUrl = url.concat(params)
      }
      //测试用url，返回值为json格式的字符串，url变量需用URLEncoder.encode(string, "UTF-8")进行转码，防止出现index编码错误
      val httpclient = new DefaultHttpClient()
      val response = httpclient.execute(new HttpGet(httpUrl))
      val entity = response.getEntity
      val result = Source.fromInputStream(entity.getContent).getLines().mkString("\n")
      httpsRequestTimes+=1
      logger.info("本次任务发出https请求：{}次",httpsRequestTimes)
      result
    } catch {
      case ex: Exception => {
        ex.printStackTrace()
        "Exception"
      }


    }
  }
}
